package com.runda.system.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bbjob.dto.Result;
import com.bbjob.model.SysOrganization;
import com.runda.system.service.OrganService;
import com.runda.util.DataEntity;


@RestController
@RequestMapping("sys/organ")
public class OrganController {
	
	@Resource
	private OrganService organservice;
	
	@RequestMapping(value = "saveOrgan", method = RequestMethod.POST)
	@ResponseBody
	public Result saveorgan(@RequestBody SysOrganization organ) {
		int ok = organservice.saveOrgan(organ);
		if(ok > 0) {
			return new Result(200, "保存成功");
		}else {
			return new Result(8106,"保存失败");
		}
	}
	
	@RequestMapping(value = "editOrgan", method = RequestMethod.POST)
	@ResponseBody
	public Result editOrgan(@RequestBody SysOrganization organ) {
		int ok = organservice.editOrgan(organ);
		if(ok > 0) {
			return new Result(200, "修改成功");
		}else {
			return new Result(8106,"修改失败");
		}
	}
	
	@RequestMapping(value = "deleteOrgan", method = RequestMethod.GET)
	@ResponseBody
	public Result deleteOrgan(Integer id) {
		int ok = organservice.deleteOrgan(id);
		if(ok > 0) {
			return new Result(200, "删除成功");
		}else {
			return new Result(8106,"删除失败");
		}
	}
	
	@RequestMapping(value = "getTree", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getTree() {
		return DataEntity.getInstance(organservice.selectTreeByPid(null));
	}
	
	/**
	 * 只获取一级组织
	 * @return
	 */
	@RequestMapping(value = "getMotherTree", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getMotherTree() {
		return DataEntity.getInstance(organservice.getMotherTree());
	}
	
	/**
	 * 根据pid只获取一层结构
	 * @param pid
	 * @return
	 */
	@RequestMapping(value = "getOrgansByPid", method = RequestMethod.POST)
	@ResponseBody
	public Result getOrgansByPid(@RequestBody SysOrganization organ) {
		
		return new Result(organservice.getOrgansByPid(organ));
	}
	
	
}
